//订货合同的页面的回调函数
function dhhtInfoFunc(data,dsId,prefix,keyValues){
	var isFinVerify = $('#fdh_addWinForm input[qucln=IS_FIN_VERIFY]').val();
	var isStock = $('#fdh_addWinForm input[qucln=IS_STOCK]').val();
	if(isFinVerify==1){   //财务审核完成，财务按钮灰化
		$('#fdh_M_UBUTN a[code=dhcwsh]').linkbutton("disable"); // 财务审核不可用
	}
	if(isStock==1){
		$('#fdh_M_UBUTN a[code=DhhtToClRK]').linkbutton("disable"); // 材料入库按钮不可用
	}
	$("#"+prefix+"_addWinTable").attr("initfunc","dhhtInfoFunc")
}

//1.添加明细的方法
function _dhht_addItem(obj){
	//获取纸张类型
	var zzType = $('#fdh_addWinTable select[qucln=DJ_TYPE]').val();
	if(zzType == '' || zzType == null){
		$.messager.alert('温馨提示','亲，请先选择纸张类型！');
		return;
	}else{
		_data_qw('dhh','ds_yz_contract_purchase_cl','dhh_sure1','44','dhhtFunc','');
	}
}

//2.添加明细回调的函数（重写查询方法）
function dhhtFunc(prefix){
	_zae_t_dg(prefix);
	//重写查询按钮的方法
	$('#'+prefix+'_commDataLeftD a[id='+prefix+'_dataQuery]').attr('onclick','dhhtItemQuery(this)');
}
//3.重写之后的查询方法
function dhhtItemQuery(obj){
	var prefix=$(obj).attr('pre');
	var dsId=$(obj).attr('dsid');
	var resultDG = 'DATA_DATALIST';
	var comUseConf='commUseD';
	var otherConfig = "";
	//获取纸张类型
	var zzType = $('#fdh_addWinTable select[qucln=DJ_TYPE]').val();
	 //获取供货商Id
    var manId = $('#fdh_addWinTable input[qucln=VENDOR_ID]').val();
    if(zzType == 'dhhtType.002'){ //特种纸
    	if(manId == "" || manId == null){
    		otherConfig = "1-DJTYPE-TZ-inc|+"; 
    	}else{
    		otherConfig = "1-DJTYPE-TZ-inc|+1-VENDOR_ID-"+manId+"-inc|+"; 
    	}
    }else if(zzType == 'dhhtType.001'){ //常规纸
    	otherConfig = "1-DJTYPE-BY-inc|+"; 
    }
	var comUse = parseConfig(prefix+"_"+comUseConf);
	var useConf = otherConfig+comUse;
	console.log(useConf);
	$('#'+prefix+"_"+resultDG).datagrid({
		url:_m_url+'framework/loadDataQ',
		queryParams:{'dsId':dsId,'config':useConf},
		onLoadSuccess:function(){
			
		}
	});
}
//获取订货合同拉取的库房
function getStockId(peItemId){
	var stockId ='';
	$.ajax({
		type:'post',
		url:_m_url+'fxa/findStockByYSCId',
		data:{'peItemId':peItemId},
		async:false,
		success:function(data){
			//返回库房Id
			stockId = data;
		}
	})
	return stockId;
}
//4.弹窗的确定按钮
function _dhh_sure1(){
	$('#dhh_DATA_PARENT_PANEL').window('close');
	var selRows = $('#dhh_DATA_DATALIST').datagrid('getSelections');
	var oldRows = $('#fdh_addItemDatagrid').datagrid('getRows');
	var oldlength=oldRows.length;
	var sortNum = getSortNum('fdh');									// 顺序号
	//获取纸张类型
	var zzType = $('#fdh_addWinTable select[qucln=DJ_TYPE]').val();
	 if(zzType == 'dhhtType.002'){ //特种纸
		 var stockId = "";
		 if(oldlength == 0){
			 for ( var i = 0; i < selRows.length; i++) {
				 //获取库房   如果库房为空就去获取，如果不为空则直接拉取
				 if(selRows[i].ARRIVAL_STOCK != '' && selRows[i].ARRIVAL_STOCK !=null){
					 stockId = selRows[i].ARRIVAL_STOCK;
				 }else{
					 stockId = getStockId(selRows[i].PE_ITEM_ID);
				 }
				 var unit = window.parent.dz_cljlUnitfmat(selRows[i].CL_UNIT);
					$('#fdh_addItemDatagrid').datagrid('appendRow', {
						CL_ID:selRows[i].CL_ID,
						//CL_STANDARD:selRows[i].CL_GUIGE,
						CL_GLEN:selRows[i].CL_GLEN,
						CL_GWID:selRows[i].CL_GWID,
						CL_WEIGHT:selRows[i].CL_WEIGHT,
						CL_UNIT:selRows[i].CL_UNIT,
						SORT_NUM:sortNum+1+i,
						XT_ID:selRows[i].XT_ID,
						EDITION_IMPRESSIONS:selRows[i].EDITION_IMPRESSIONS,
						PUB_ID:selRows[i].PUB_ID,
						CL_AMOUNT_YSFY:selRows[i].TOTALAMOUNT+unit,
						CL_AMOUNT:selRows[i].TOTALAMOUNT,
						//ARRIVAL_STOCK:stockId,
						SOURCE_ID:selRows[i].PE_ITEM_ID
					})
				}
		}else{
			for ( var j = 0; j < selRows.length; j++) {
				 //获取库房   如果库房为空就去获取，如果不为空则直接拉取
				if(selRows[j].ARRIVAL_STOCK != '' && selRows[j].ARRIVAL_STOCK !=null){
					stockId = selRows[j].ARRIVAL_STOCK;
				}else{
					stockId = getStockId(selRows[j].PE_ITEM_ID);
				}
				for(var k = 0; k < oldlength;k++){
					if(selRows[j].PE_ITEM_ID == oldRows[k].SOURCE_ID ){
						break;
					}else{
						if(k==(oldlength-1)){
							var unit = window.parent.dz_cljlUnitfmat(selRows[j].CL_UNIT);
							$('#fdh_addItemDatagrid').datagrid('appendRow', {
								CL_ID:selRows[j].CL_ID,
								//CL_STANDARD:selRows[j].CL_GUIGE,
								CL_GLEN:selRows[j].CL_GLEN,
								CL_GWID:selRows[j].CL_GWID,
								CL_WEIGHT:selRows[j].CL_WEIGHT,
								CL_UNIT:selRows[j].CL_UNIT,
								SORT_NUM:sortNum+1+j,
								XT_ID:selRows[j].XT_ID,
								EDITION_IMPRESSIONS:selRows[j].EDITION_IMPRESSIONS,
								PUB_ID:selRows[j].PUB_ID,
								CL_AMOUNT_YSFY:selRows[j].TOTALAMOUNT+unit,
								CL_AMOUNT:selRows[j].TOTALAMOUNT,
								ARRIVAL_STOCK:stockId,
								SOURCE_ID:selRows[j].PE_ITEM_ID
							})
						}
					}
				}
			}
		}
	 }else if(zzType == 'dhhtType.001'){ //常规纸
		 for ( var i = 0; i < selRows.length; i++) {
			 var unit = window.parent.dz_cljlUnitfmat(selRows[i].CL_UNIT);
				$('#fdh_addItemDatagrid').datagrid('appendRow', {
					CL_ID:selRows[i].CL_ID,
					//CL_STANDARD:selRows[i].CL_GUIGE,
					CL_GLEN:selRows[i].CL_GLEN,
					CL_GWID:selRows[i].CL_GWID,
					CL_WEIGHT:selRows[i].CL_WEIGHT,
					CL_UNIT:selRows[i].CL_UNIT,
					SORT_NUM:sortNum+1+i,
					XT_ID:selRows[i].XT_ID,
					EDITION_IMPRESSIONS:selRows[i].EDITION_IMPRESSIONS,
					PUB_ID:selRows[i].PUB_ID,
					CL_AMOUNT_YSFY:selRows[i].TOTALAMOUNT+unit,
					CL_AMOUNT:selRows[i].TOTALAMOUNT,
					ARRIVAL_STOCK:selRows[i].ARRIVAL_STOCK,
					SOURCE_ID:selRows[i].PE_ITEM_ID
				})
			}
	 }
	var newlength=oldRows.length;
	totalcalcu("fdh",oldlength,newlength);
}

